package com.briup.cms.service.impl;

import com.briup.cms.bean.Comment;
import com.briup.cms.dao.CommentMapper;
import com.briup.cms.dao.extend.CommentExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.ICommentService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论服务层
 */
@Service
public class CommentServiceImpl implements ICommentService {
	@Resource
	private CommentMapper commentMapper;
	@Resource
	private CommentExtendMapper CommentExtendMapper;

	// 分页查询所有评论
	@Override
	public PageInfo<Comment> pageQuery(int pageNum, int pageSize, String keywords) {
		PageHelper.startPage(pageNum,pageSize,true);
		List<Comment> list = CommentExtendMapper.pageQuery(keywords);
		return new PageInfo<>(list);
	}

	// 通过id删除评论
	@Transactional
	@Override
	public void deleteById(int id) throws CustomerException {
		commentMapper.deleteByPrimaryKey(id);
	}

	// 评论删除评论
	@Transactional
	@Override
	public void batchDelete(int[] ids) throws CustomerException {
		if(ids!=null && ids.length>0) {
			for(int id : ids) {
				commentMapper.deleteByPrimaryKey(id);
			}
		}
	}

	// 审核评论，状态：0-待审核，1-不通过，2-通过
	@Transactional
	@Override
	public void checkComment(int id, int status) throws CustomerException {
		Comment commentFromDB = commentMapper.selectByPrimaryKey(id);
		if(commentFromDB == null){
			throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_COMMENT_NOT_EXIST);
		}
		Comment comment = new Comment();
		comment.setCommentId(id);
		comment.setCommentStatus(status);
		commentMapper.updateByPrimaryKeySelective(comment);
	}
}

